package kevin.framework.basic.common.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.utils.IControlCommon;

/**
 * 封装datagrid的配置
 * 
 * @author kevin.huang
 *
 */
public class DataGrid {
	private String id;
	private String methodsObject;
	private int pagesize;
	private int colfield;// 多行列头时，字段所在的数组索引
	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getLineBtnC() {
		if (lineButton == null) {
			return 0;
		}
		return lineButton.size();
	}

	public int getColfield() {
		return colfield;
	}

	public void setColfield(int colfield) {
		this.colfield = colfield;
	}

	private boolean oprCol = false;
	private boolean checkbox = true;
	private List<ClientButton> toolbar = null;
	private List<ClientButton> lineButton = null;
	private String colString;
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String toJson(HttpServletRequest request, IControlCommon authValid) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		buffer.append("\"id\":\"" + getId() + "\",");
		buffer.append("\"url\":\"" + getUrl() + "\",");
		buffer.append("\"title\":\"" + getTitle() + "\",");
		buffer.append("\"colfield\":" + getColfield() + ",");
		buffer.append("\"methodsObject\":\"" + getMethodsObject() + "\",");
		buffer.append("\"pageSize\":" + getPagesize() + ",");
		buffer.append("\"oprCol\":" + isOprCol() + ",");
		buffer.append("\"checkBox\":" + isCheckbox() + ",");
		buffer.append("\"cols\":" + getColString() + ",");
		buffer.append("\"trBtnCount\":" + getLineBtnC() + ",");
		buffer.append("\"toolbar\":[");
		if (toolbar != null) {
			for (int i = 0, len = toolbar.size(); i < len; ++i) {
				ClientButton c = toolbar.get(i);
				JSONObject jObj = c.toJSONObject(request, authValid, null);
				/*
				 * if(authValid.hasPrivilage(request, c.getCmd())){ c.setParams("privilage",
				 * "1"); c.setDisabled(false); }else{ c.setParams("privilage", "0");
				 * c.setDisabled(true); }
				 */
				buffer.append(jObj.toJSONString());
				if (i < len - 1) {
					buffer.append(",");
				}
			}
		}
		buffer.append("]}");
		return buffer.toString();
	}

	public String getLineBtns(HttpServletRequest request, IControlCommon authValid) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"lineButton\":[");
		if (lineButton != null) {
			for (int i = 0, len = lineButton.size(); i < len; ++i) {
				ClientButton c = lineButton.get(i);
				JSONObject jObj = c.toJSONObject(request, authValid, null);
				/*
				 * if(authValid.hasPrivilage(request, c.getCmd())) c.setDisabled(false); else
				 * c.setDisabled(true);
				 */
				buffer.append(jObj.toJSONString());
				if (i < len - 1) {
					buffer.append(",");
				}
			}
		}
		buffer.append("]}");
		return buffer.toString();
	}

	public String getId() {
		return id == null ? "" : id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMethodsObject() {
		return methodsObject == null ? "methodsObject" : methodsObject;
	}

	public void setMethodsObject(String methodsObject) {
		this.methodsObject = methodsObject;
	}

	public int getPagesize() {
		return pagesize == 0 ? 20 : pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public boolean isOprCol() {
		return oprCol;
	}

	public void setOprCol(boolean oprCol) {
		this.oprCol = oprCol;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	public List<ClientButton> getToolbar() {
		return toolbar;
	}

	public void setToolbar(List<ClientButton> toolbar) {
		this.toolbar = toolbar;
	}

	public List<ClientButton> getLineButton() {
		return lineButton;
	}

	public void setLineButton(List<ClientButton> lineButton) {
		this.lineButton = lineButton;
	}

	public String getColString() {
		return colString;
	}

	public void setColString(String colString) {
		this.colString = colString;
	}

	public Map<String, String> getAllCmdDesp() {
		Map<String, String> list = new HashMap<String, String>();
		if (toolbar != null) {
			for (ClientButton btn : toolbar) {
				String desp = btn.getText();
				String cmd = btn.getCmd();
				if (!list.containsKey(desp)) {
					list.put(desp, cmd);
				}
			}
		}
		if (lineButton != null) {
			for (ClientButton btn : lineButton) {
				String desp = btn.getText();
				String cmd = btn.getCmd();
				if (!list.containsKey(desp)) {
					list.put(desp, cmd);
				}
			}
		}
		return list;
	}

	public Set<String> getAllCmd() {
		Set<String> res = new HashSet<String>();
		if (toolbar != null) {
			for (ClientButton btn : toolbar) {
				if (!res.contains(btn.getCmd())) {
					res.add(btn.getCmd());
				}
			}
		}
		if (lineButton != null) {
			for (ClientButton btn : lineButton) {
				if (!res.contains(btn.getCmd())) {
					res.add(btn.getCmd());
				}
			}
		}
		return res;
	}
}
